﻿var MangerUsersCreate = function () {

    // basic validation
    var handleCreate = function () {
        // for more info visit the official plugin documentation: 
        // http://docs.jquery.com/Plugins/Validation

        var errobj = $(".validation-summary-errors");
        var errstr = errobj.find("li").eq(0).text();
        if (errstr.length > 0) {
            errobj.html("<button class=\"close\" data-close=\"alert\"></button><span>" + errstr + ".</span>");
            errobj.addClass("alert alert-danger").removeClass("validation-summary-errors");
        }

        var form = $('#formmangeruserscreate');
        var error = $('.alert-danger', form);
        var success = $('.alert-success', form);

        form.validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block help-block-error', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "", // validate all fields including form hidden input
            messages: {
                UserName: {
                    required: '用户名不能为空'
                },
                Password: {
                    minlength: '密码最少6个字符',
                    required: '密码不能为空'
                },
                ConfirmPassword: {
                    required: '确认密码不能为空',
                    equalTo: '确认密码不致'
                }
            },
            rules: {
                UserName: {
                    required: true
                },
                Password: {
                    minlength: 6,
                    required: true
                },
                ConfirmPassword: {
                    required: true,
                    equalTo: "#Password"
                }
            },

            errorPlacement: function (error, element) {
                if (element.attr("name") === "fileicon") {
                    error.insertAfter(element.parent());
                } else {
                    error.insertAfter(element);
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit              
                success.hide();
                error.show();
                Metronic.scrollTo(error, -200);
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            unhighlight: function (element) { // revert the change done by hightlight
                $(element)
                    .closest('.form-group').removeClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label
                    .closest('.form-group').removeClass('has-error'); // set success class to the control group
            },

            submitHandler: function (form) {
                //success.show();
                error.hide();

                form.submit();
            }
        });
    }
    return {
        //main function to initiate the module
        init: function () {
            handleCreate();
        }
    };
}();

var MangerUsersEdit = function () {

    // basic validation
    var handleCreate = function () {
        // for more info visit the official plugin documentation: 
        // http://docs.jquery.com/Plugins/Validation

        var errobj = $(".validation-summary-errors");
        var errstr = errobj.find("li").eq(0).text();
        if (errstr.length > 0) {
            errobj.html("<button class=\"close\" data-close=\"alert\"></button><span>" + errstr + ".</span>");
            errobj.addClass("alert alert-danger").removeClass("validation-summary-errors");
        }

        var form = $('#formmangerusersedit');
        var error = $('.alert-danger', form);
        var success = $('.alert-success', form);

        form.validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block help-block-error', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "", // validate all fields including form hidden input
            messages: {
               
                UserName: {
                    required: '用户名不能为空'
                },
                Password: {
                    minlength: '密码最少6个字符'
                },
                ConfirmPassword: {
                    equalTo: '确认密码不致'
                }
            },
            rules: {
              
                Password: {
                    minlength: 6
                },
                ConfirmPassword: {
                    equalTo: "#Password"
                }
            },

            errorPlacement: function (error, element) {
                if (element.attr("name") === "fileicon") {
                    error.insertAfter(element.parent());
                } else {
                    error.insertAfter(element);
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit              
                success.hide();
                error.show();
                Metronic.scrollTo(error, -200);
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            unhighlight: function (element) { // revert the change done by hightlight
                $(element)
                    .closest('.form-group').removeClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label
                    .closest('.form-group').removeClass('has-error'); // set success class to the control group
            },

            submitHandler: function (form) {
                //success.show();
                //if ($("#Password").val().length > 0) {
                //    $("#PassWord").val($("#Password").val());
                //}
                error.hide();
                form.submit();
            }
        });
    }
    return {
        //main function to initiate the module
        init: function () {
            handleCreate();
            //handleArea();
        }
    };
}();

var MangerUsersList = function () {

    var initList = function () {

        var table = $('#mangerusersList');

        // begin first table
        //table.dataTable({

        //    // Internationalisation. For more info refer to http://datatables.net/manual/i18n
        //    "language": {
        //        "search": "搜索: ",
        //        "emptyTable": "没有满足条件的数据",
        //        "infoEmpty": "共有0条数据",
        //        "lengthMenu": "  _MENU_ 记录",
        //        "info": "显示 _START_ 到 _END_ 共 _TOTAL_ 条记录",
        //        "zeroRecords": "没有满足该条件的数据",
        //        "paginate": {
        //            "previous": "上一页",
        //            "next": "下一页",
        //            "last": "最后一页",
        //            "first": "第一页"
        //        },
        //        "aria": {
        //            "sortAscending": ": activate to sort column ascending",
        //            "sortDescending": ": activate to sort column descending"
        //        }
        //    },
        //    "bSort": false,

        //    // Or you can use remote translation file
        //    //"language": {
        //    //   url: '//cdn.datatables.net/plug-ins/3cfcc339e89/i18n/Portuguese.json'
        //    //},

        //    // Uncomment below line("dom" parameter) to fix the dropdown overflow issue in the datatable cells. The default datatable layout
        //    // setup uses scrollable div(table-scrollable) with overflow:auto to enable vertical scroll(see: assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js). 
        //    // So when dropdowns used the scrollable div should be removed. 
        //    //"dom": "<'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r>t<'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>",

        //    "bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.

        //    "columns": [{
        //        "orderable": false
        //    }, {
        //        "orderable": true
        //    }, {
        //        "orderable": false
        //    }, {
        //        "orderable": false
        //    }, {
        //        "orderable": false
        //    }],
        //    "lengthMenu": [
        //        [5, 15, 20, -1],
        //        [5, 15, 20, "All"] // change per page values here
        //    ],
        //    // set the initial value
        //    "pageLength": 20,
        //    "pagingType": "bootstrap_full_number",
        //    "columnDefs": [{ // set default column settings
        //        'orderable': false,
        //        'targets': [0]
        //    }, {
        //        "searchable": false,
        //        "targets": [0]
        //    }],
        //    "order": [
        //        [1, "asc"]
        //    ] // set first column as a default sort by asc
        //});

        //var tableWrapper = jQuery('#sample_1_wrapper');

        table.find('.group-checkable').change(function () {
            var set = jQuery(this).attr("data-set");
            var checked = jQuery(this).is(":checked");
            jQuery(set).each(function () {
                if (checked) {
                    $(this).attr("checked", true);
                    $(this).parents('tr').addClass("active");
                } else {
                    $(this).attr("checked", false);
                    $(this).parents('tr').removeClass("active");
                }
            });
            jQuery.uniform.update(set);
        });

        table.on('change', 'tbody tr .checkboxes', function () {
            $(this).parents('tr').toggleClass("active");
        });

       // tableWrapper.find('.dataTables_length select').addClass("form-control input-xsmall input-inline"); // modify table per page dropdown
    }

    return {

        //main function to initiate the module
        init: function () {
            if (!jQuery().dataTable) {
                return;
            }
            initList();
        }

    };

}();